package signals

import (
	"os"
	"fmt"
)

type signalHandler func(s os.Signal, arg interface{})

type signalSet struct {
	M map[os.Signal]signalHandler
}

func SignalSetNew() (*signalSet) {
	ss := new(signalSet)
	ss.M = make(map[os.Signal]signalHandler)
	return ss
}

func (s *signalSet) Register(signal os.Signal, handler signalHandler) error{
	if _,ok := s.M[signal]; ok {
		fmt.Errorf("sifnal: Register signal name twice for %s", signal.String())
	}
	s.M[signal] = handler
	return nil
}

func (s *signalSet) Handle(signal os.Signal, arg interface{}) (err error)  {
	if _,ok := s.M[signal]; ok{
		s.M[signal](signal, arg)
		return nil
	}else {
		return fmt.Errorf("No handler available for signal %v", signal)
	}
}

